<?php

namespace App\Console\Commands;

use App\Model\Number;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class SendCustomMsgCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * type int 0.系统消息 1.警告消息
     * all int 0.非全员 1.全员
     * msg string 消息内容
     * user array 用户ID
     * @var string
     */
    protected $signature = 'send:custom:msg {type} {all} {msg} {user?} {link?} {link_tip?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $msg = (string) $this->argument('msg');
        $uuid = $this->argument('user');
        $type = (int) $this->argument('type');
        $all = (int) $this->argument('all');
        $link = (string) $this->argument('link');
        $link_tip = (string) $this->argument('link_tip');
        if($type === 0){
            // 全员
            if($all === 1){
//                $numbers = Number::whereNotIn('user_id',[11,12,13,14])->get(['user_id'])->toArray();
                $this->sendPushMsg($msg);
            } else {
                $numbers = Number::whereIn('uuid', explode(',', $uuid))->get(['user_id'])->toArray();
                $user = array_column($numbers,'user_id');
                $chunkUsers = collect($user)->chunk(100)->toArray();
                foreach ($chunkUsers as $numbers) {
                    $this->send_sys_msg($numbers, $msg, $link, $link_tip);
                    sleep(1);
                }
            }
        } elseif($type === 1){
            if($all === 0){
                $numbers = Number::whereIn('uuid', explode(',', $uuid))->get(['user_id'])->toArray();
                $user = array_column($numbers,'user_id');
                $this->send_warning_msg($user, $msg, $link, $link_tip);
            } elseif ($all === 1){
                $this->send_all_warning_msg($msg);
            }
        }
    }

    /**
     * 系统消息
     * @param $user_id
     * @param $msg
     */
    private function send_sys_msg(array $user_id, string $msg, $link='', $link_tip='')
    {
        curl_request(getKkaudioApiUrl()."/inner/msg/text", ['user_id' => $user_id, 'msg' => $msg, 'link'=>$link, 'link_tip'=>$link_tip], 'json');
    }

    /**
     * 单条警告消息
     * @param array $user
     * @param string $msg
     */
    private function send_warning_msg(array $user, string $msg, $link='', $link_tip=''){
        $res = curl_request(getKkaudioApiUrl()."/inner/msg/warning_msg", [
            'user_id' => $user[0],
            'title' => '警告消息',
            'link' => $link,
            'link_tip' => $link_tip,
            'content' => $msg
        ]);
    }

    /**
     * 全服警告消息
     * @param string $msg
     */
    private function send_all_warning_msg(string $msg){
        curl_request(getKkaudioApiUrl()."/inner/msg/all_warning_msg",[ 'title' => '警告消息', 'content' => $msg ]);
    }

    public function sendPushMsg($msg){
//        curl_request(getKkaudioApiUrl()."/inner/msg/pushMsg",['msg' => $msg]);
        curl_request("http://39.105.149.236:5084/inner/msg/pushMsg",['msg' => $msg]);
    }
}
